import { OneNETAPI } from "../../utils/OneNETAPI/OneNETAPI"
import * as echarts from '../../ec-canvas/echarts'
const app = getApp<IAppOption>()
const OneNET = new OneNETAPI('sl5gkKWYKYCShqiS7egDu==pJ3c=')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      lazyLoad: true
    },
    state: 0,
    dateStart: '',
    dateEnd: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var date = new Date()
    var m: any = date.getMonth() + 1
    var d: any = date.getDate()
    m = m > 9 ? m : '0' + m
    d = d > 9 ? d : '0' + d
    var today = date.getFullYear() + '-' + m + '-' + d
    this.setData({ dateEnd: today })

    date = new Date(date.getTime() - 7 * 24 * 60 * 60 * 1000)
    m = date.getMonth() + 1
    d = date.getDate()
    m = m > 9 ? m : '0' + m
    d = d > 9 ? d : '0' + d
    var before7 = date.getFullYear() + '-' + m + '-' + d
    // var before7 = 2020 + '-' + m + '-' + d
    this.setData({ dateStart: before7 })
  },

  bJumpTap: function (options) {
    wx.navigateTo({
      url: '../Reply/Reply'
    })
  },

  onDateChange: function (e) {
    var obj: any = {}
    obj[e.currentTarget.id] = e.detail.value
    this.setData(obj)
    this.updateDeviceData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  ecComponent: {} as AnyObject,
  chart: {} as AnyObject,
  updateDeviceData: function () {
    // const devId = '621232537'
    const devId = app.globalData.UserId
    var opt = {
      title: { text: '设备数据', left: 'center' },
      grid: { containLabel: true },
      tooltip: { show: true, trigger: 'axis' },
      yAxis: {
        x: 'center',
        type: 'value',
        splitLine: { lineStyle: { type: 'dashed' } }
      },

      legend: {
        data: [] as Array<string>,
        top: 50,
        left: 'center',
        z: 100
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: [] as Array<string>,
      },
      series: [] as Array<any>
    }
    OneNET.getDataPoints({
      device_id: devId,
      args: {
        start: this.data.dateStart + 'T00:00:00',
        end: this.data.dateEnd + 'T23:59:59',
        limit: 40
      },
      successcb: (res) => {
        console.log('历史数据', res)
        if (res.errno == 0) {
          let datastrams = res.data.datastreams
          for (let datastream of datastrams) {
            opt.legend.data.push(datastream.id)
            var ser = {
              name: datastream.id,
              type: 'line',
              smooth: true,
              data: [] as Array<number>
            }
            for (let datapoint of datastream.datapoints)
              ser.data.push(datapoint.value)
            opt.series.push(ser)
          }
          this.setData({ state: 1 })
          for (let datastream of datastrams) {
            for (let datapoint of datastream.datapoints) {
              let date: string[] = datapoint.at.split(' ')
              opt.xAxis.data.push(date[0])
            }
            break
          }
          console.log(opt)
          this.chart.setOption(opt, true)
        }
        else
          this.setData({ state: 2 })
      },
      failcb: (res) => {
        console.log(res)
        this.setData({ state: 2 })
      }
    })
  },

  onReady: function () {
    this.ecComponent = this.selectComponent('#mychart-dom-bar')
    this.ecComponent.init((canvas, width, height, dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
      });
      this.chart = chart;
      canvas.setChart(chart);
      this.updateDeviceData()
      return chart;
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})